/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.Item;
import utility.NameValueList;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.ItemPerfoma;

/**
 *
 * @author jay
 */
public class ItemTM extends AbstractTableModel {
    
  //  private static final long serialVersionUID = 1L;\
  
    private List<Item> data;
    private List<NameValueList> performList = EnumUtils.enumToNameValueList(ItemPerfoma.class);

    public ItemTM(List<Item> data) {
        this.data = data;
    }

    public ItemTM() {
        this(new ArrayList<Item>(0));
    }

    public Item get(int index) {
        return data.get(index);
    }

    public void add(Item product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, Item product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<Item> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
         
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 11;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: if(data.get(rowIndex).getDept() != null) {
                        return data.get(rowIndex).getDept().getKode();
                    } else {return null;}    
            case 2: return data.get(rowIndex).getPlu();
            case 3: return data.get(rowIndex).getBarcode();
            case 4: return data.get(rowIndex).getNama();
            case 5: if(data.get(rowIndex).getDivOrder() != null) {
                        return data.get(rowIndex).getDivOrder();
                    } else {return null;}
                
            case 6: if(data.get(rowIndex).getPerforma() != null) {
                        int i = NameValueList.indexOf(performList, data.get(rowIndex).getPerforma());
                        return performList.get(i).getValue();
                    } else {return null;}
                
            case 7: if(data.get(rowIndex).getStokMax() != null) {
                        return data.get(rowIndex).getStokMax();
                    } else {return null;}
                
            case 8: if(data.get(rowIndex).getStokMin() != null) {
                        return data.get(rowIndex).getStokMin();
                    } else {return null;}
                
            case 9: if(data.get(rowIndex).getStokX() != null) {
                        return data.get(rowIndex).getStokX();
                    } else {return null;}
                
            
            
            default: return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";// + (column==sortCol ? (isSortAsc ? " >>":" <<"):"");
            case 1: return "KATEGORI";
            case 2: return "PLU";
            case 3: return "BARCODE";
            case 4: return "NAMA";
            case 5: return "DIV";
            case 6: return "TAG";
            case 7: return "MAX.STOK";
            case 8: return "MIN.STOK";
            case 9: return "MIN.X";
            default: return null;
        }
    }
    
//    class ColumnListener extends MouseAdapter {
//    protected JTable table;
//
//    public ColumnListener(JTable t) {
//      table = t;
//    }
//
//    @Override
//    public void mouseClicked(MouseEvent e) {
//        TableColumnModel colModel = table.getColumnModel();
//        int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
//        int modelIndex = colModel.getColumn(columnModelIndex)
//            .getModelIndex();
//
//        if (modelIndex < 0) { return; }
//        if (sortCol == modelIndex) { isSortAsc = !isSortAsc; }
//        else { sortCol = modelIndex; }
//
//        for (int i = 0; i < columnsCount; i++) { 
//          TableColumn column = colModel.getColumn(i);
//          column.setHeaderValue(getColumnName(column.getModelIndex()));
//        }
//        table.getTableHeader().repaint();
//
////      Collections.sort(data,new TableColumnComparator(isSortAsc));
////      table.tableChanged(new TableModelEvent(MyTableModel.this));
////      table.repaint();
//    }
//  }
    
}
